﻿Imports System.Data
Imports log4net

Partial Class MassChange
    Inherits System.Web.UI.Page

    Public scripttext As String
    Private Shared logger As ILog = LogManager.GetLogger("MassChange")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If

        'Session("users") = "bb0011"
        'Session("PlanYear") = "2557"
        PlanYearsTxt.Text = Session("PlanYear")

        'If Session("users") Is Nothing Then
        '    Response.Redirect("LoginForm.aspx")
        'End If
        GenerateValues()
        Label1.Text = "ระบบจัดทำงบประมาณ > จัดทำงบประมาณ > Mass Change"

        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable

        Dim filter As String = ""
        filter = FctrClass.GetSubFundCenter("0000", Session("users"), Session("PlanYear"), "")
        Session.Remove("prefilter")
        Session.Add("prefilter", filter)
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)

        ms8.DataValueField = "FundCenterCode"
        ms8.DataTextField = "Description"
        ms8.DataSource = dt
        ms8.DataBind()

        Dim adapter As New MassChangeDSTableAdapters.CmmtsTableAdapter
        Dim datatable As New MassChangeDS.CmmtsDataTable
        datatable = adapter.GetData(Session("PlanYear"))
        ms9.DataValueField = "CmmtCode"
        ms9.DataTextField = "Description"
        ms9.DataSource = datatable
        ms9.DataBind()
        GenerateValues()
    End Sub
    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt
        WebGrid1.ClientAction.Alert(sql)
    End Sub
    Protected Sub WebCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt

    End Sub
    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MassChangeDSTableAdapters.CmmtsTableAdapter
        Dim dt As New MassChangeDS.CmmtsDataTable

        dt = adap.GetData(Session("PlanYear"))

        e.DataSource = dt



    End Sub
    Protected Sub WebCombo4_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MassChangeDSTableAdapters.CmmtsTableAdapter
        Dim dt As New MassChangeDS.CmmtsDataTable

        dt = adap.GetData(Session("PlanYear"))

        e.DataSource = dt



    End Sub
    Protected Sub Project_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        'Dim adap As New MassChangeDSTableAdapters.ProjectsTableAdapter
        'Dim dt As New MassChangeDS.ProjectsDataTable

        'dt = adap.GetData(Session("PlanYear"))

        Dim adapter As New AttachmentTableAdapters.ProjectsTableAdapter
        Dim dt As New Attachment.ProjectsDataTable
        'data = adapter.GetDataByPlanYear(Val(Session("PlanYear")))
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " AND PlanYear = " & PlanYearsTxt.Text & " "
        If prefil <> "" Then
            sql += " AND FundCenter_FundCenterCode IN (" & prefil & ") "
        End If
        sql += " UNION  "
        sql += " SELECT ProjectCode, PlanYear, (Description) AS Description , CreatedDate, UpdatedDate, CreatedBy, UpdatedBy, FundCenter_FundCenterCode "
        sql += " FROM Projects AS Projects1 "
        sql += " WHERE Projects1.FundCenter_FundCenterCode IS NULL "
        sql += " AND Projects1.PlanYear = " & PlanYearsTxt.Text & " "

        adapter.GetDataByFilter(dt, sql)

        e.DataSource = dt



    End Sub
    Protected Sub ActType_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MassChangeDSTableAdapters.ActivityTypesTableAdapter
        Dim dt As New MassChangeDS.ActivityTypesDataTable

        dt = adap.GetData(Session("PlanYear"))

        e.DataSource = dt

    End Sub
    Protected Sub Product_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MassChangeDSTableAdapters.ProductsTableAdapter
        Dim dt As New MassChangeDS.ProductsDataTable

        dt = adap.GetData(Session("PlanYear"))

        e.DataSource = dt

    End Sub
    Protected Sub Activity_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MassChangeDSTableAdapters.ActivitiesTableAdapter
        Dim dt As New MassChangeDS.ActivitiesDataTable

        dt = adap.GetData(Session("PlanYear"))

        e.DataSource = dt

    End Sub
    Protected Sub Item_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim ds As New DataSet
        Dim dt As DataTable = ds.Tables.Add("Items")
        dt.Columns.Add("RecType", Type.GetType("System.String"))
        dt.Columns.Add("Description", Type.GetType("System.String"))
        Dim r As DataRow
        r = dt.NewRow()
        r("RecType") = "Fct"
        r("Description") = "Fund Center"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("RecType") = "Cmt"
        r("Description") = "Commitment Item"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("RecType") = "Pjt"
        r("Description") = "Project"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("RecType") = "Atp"
        r("Description") = "Activity Type"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("RecType") = "Prd"
        r("Description") = "Product"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("RecType") = "Act"
        r("Description") = "Activity"
        dt.Rows.Add(r)

        e.DataSource = dt

    End Sub
    Protected Sub ItemList_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MassChangeDSTableAdapters.ItemTableAdapter
        Dim dt As New MassChangeDS.ItemDataTable

        dt = adap.GetData(Session("PlanYear"))

        e.DataSource = dt

    End Sub
    Protected Sub AmountChange_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim ds As New DataSet
        Dim dt As DataTable = ds.Tables.Add("Items")
        dt.Columns.Add("RecType", Type.GetType("System.String"))
        dt.Columns.Add("Description", Type.GetType("System.String"))
        Dim r As DataRow
        r = dt.NewRow()
        r("RecType") = "T1"
        r("Description") = "เปลี่ยนเป็น (บาท)"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("RecType") = "T2"
        r("Description") = "เพิ่มขึ้น (%)"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("RecType") = "T3"
        r("Description") = "ลดลง (%)"
        dt.Rows.Add(r)



        e.DataSource = dt

    End Sub
    Sub GenerateValues()
        If hdf1.Value.Length = 0 And hdf2.Value.Length = 0 Then
            Session("Search") = "false"
        End If

        Try
            Dim Numx As Array = hdf1.Value.Split(",")
            For i = 0 To Numx.Length - 1
                ms8.Items.FindByValue(Numx(i).ToString).Selected = True
            Next
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)
        End Try
        Try
            Dim Numx2 As Array = hdf2.Value.Split(",")
            For j = 0 To Numx2.Length - 1
                ms9.Items.FindByValue(Numx2(j).ToString).Selected = True
            Next
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)
        End Try
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        If VersionTxt.Text.Trim = "" Then
            scripttext = "<script>alert('กรุณาระบุ Version');</script>"
            Exit Sub
        End If
        Session("test") = 0
        GenerateValues()

        GetData()

    End Sub
    Sub GetData()
        Dim adapter As New MassChangeDSTableAdapters.BudgetDetailsTableAdapter
        Dim datatable As New MassChangeDS.BudgetDetailsDataTable
        'showd = showdata.GetChild(id, Session("users"))

        Dim sql As String = ""
        sql = ComposeSQL()
        'Response.Write(sql)
        adapter.GetDataByFilter(datatable, sql)

        WebGrid1.DataSource = datatable
        WebGrid1.DataBind()
    End Sub
    Function ComposeSQL() As String
        Dim ValList As String
        Dim sql As String = ""
        sql += " WHERE 1 = 1 "
        'sql += " and Amount <> 0 "
        sql += " and planyear = " & PlanYearsTxt.Text
        sql += " and version = '" & VersionTxt.Text & "'"
        sql += " and fundcenter_fundcentercode IN (" & Session("prefilter") & ")"
        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            If WebCombo1.Value <> "" And WebCombo2.Value <> "" Then
                sql += " and fundcenter_fundcentercode >= '" & WebCombo1.Value & "' and fundcenter_fundcentercode <= '" & WebCombo2.Value & "'"
            ElseIf WebCombo1.Value <> "" And WebCombo2.Value = "" Then
                sql += " and fundcenter_fundcentercode >= '" & WebCombo1.Value & "' and fundcenter_fundcentercode <= '" & WebCombo1.Value & "'"
            ElseIf WebCombo1.Value = "" And WebCombo2.Value <> "" Then
                sql += " and fundcenter_fundcentercode >= '" & WebCombo2.Value & "' and fundcenter_fundcentercode <= '" & WebCombo2.Value & "'"
            End If
        Else
            If hdf1.Value <> "" Then
                ValList = "('" & Replace(hdf1.Value, ",", "','") & "')"
                sql += " and fundcenter_fundcentercode in " & ValList
            End If
        End If
        If TabContainer2.ActiveTab.ID = TabPanel3.ID Then
            If WebCombo3.Value <> "" And WebCombo4.Value <> "" Then
                sql += " and cmmt_cmmtcode >= '" & WebCombo3.Value & "' and cmmt_cmmtcode <= '" & WebCombo4.Value & "'"
            ElseIf WebCombo3.Value <> "" And WebCombo4.Value = "" Then
                sql += " and cmmt_cmmtcode >= '" & WebCombo3.Value & "' and cmmt_cmmtcode <= '" & WebCombo3.Value & "'"
            ElseIf WebCombo3.Value = "" And WebCombo4.Value <> "" Then
                sql += " and cmmt_cmmtcode >= '" & WebCombo4.Value & "' and cmmt_cmmtcode <= '" & WebCombo4.Value & "'"
            End If
        Else
            If hdf2.Value <> "" Then
                ValList = "('" & Replace(hdf2.Value, ",", "','") & "')"
                sql += " and cmmt_cmmtcode in " & ValList
            End If
        End If

        If ProjectCombo1.Value <> "" And ProjectCombo2.Value <> "" Then
            sql += " and project_projectcode >= '" & ProjectCombo1.Value & "' and project_projectcode <= '" & ProjectCombo2.Value & "'"
        ElseIf ProjectCombo1.Value <> "" And ProjectCombo2.Value = "" Then
            sql += " and project_projectcode >= '" & ProjectCombo1.Value & "' and project_projectcode <= '" & ProjectCombo1.Value & "'"
        ElseIf ProjectCombo1.Value = "" And ProjectCombo2.Value <> "" Then
            sql += " and project_projectcode >= '" & ProjectCombo2.Value & "' and project_projectcode <= '" & ProjectCombo2.Value & "'"
        End If

        If ActTypeCombo1.Value <> "" And ActTypeCombo2.Value <> "" Then
            sql += " and activitytype_activitytypecode >= '" & ActTypeCombo1.Value & "' and activitytype_activitytypecode <= '" & ActTypeCombo2.Value & "'"
        ElseIf ActTypeCombo1.Value <> "" And ActTypeCombo2.Value = "" Then
            sql += " and activitytype_activitytypecode >= '" & ActTypeCombo1.Value & "' and activitytype_activitytypecode <= '" & ActTypeCombo1.Value & "'"
        ElseIf ActTypeCombo1.Value = "" And ActTypeCombo2.Value <> "" Then
            sql += " and activitytype_activitytypecode >= '" & ActTypeCombo2.Value & "' and activitytype_activitytypecode <= '" & ActTypeCombo2.Value & "'"
        End If

        If ProductCombo1.Value <> "" And ProductCombo2.Value <> "" Then
            sql += " and product_productcode >= '" & ProductCombo1.Value & "' and product_productcode <= '" & ProductCombo2.Value & "'"
        ElseIf ProductCombo1.Value <> "" And ProductCombo2.Value = "" Then
            sql += " and product_productcode >= '" & ProductCombo1.Value & "' and product_productcode <= '" & ProductCombo1.Value & "'"
        ElseIf ProductCombo1.Value = "" And ProductCombo2.Value <> "" Then
            sql += " and product_productcode >= '" & ProductCombo2.Value & "' and product_productcode <= '" & ProductCombo2.Value & "'"
        End If

        If ActivityCombo1.Value <> "" And ActivityCombo2.Value <> "" Then
            sql += " and activity_activitycode >= '" & ActivityCombo1.Value & "' and activity_activitycode <= '" & ActivityCombo2.Value & "'"
        ElseIf ActivityCombo1.Value <> "" And ActivityCombo2.Value = "" Then
            sql += " and activity_activitycode >= '" & ActivityCombo1.Value & "' and activity_activitycode <= '" & ActivityCombo1.Value & "'"
        ElseIf ActivityCombo1.Value = "" And ActivityCombo2.Value <> "" Then
            sql += " and activity_activitycode >= '" & ActivityCombo2.Value & "' and activity_activitycode <= '" & ActivityCombo2.Value & "'"
        End If
        'Response.Write(sql)
        Return sql
    End Function
    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs) Handles WebGrid1.InitializeRow
        'Dim NewQuantity As String = e.Row.Cells.GetNamedItem("NewQuantity").Text
        'If NewQuantity <> "" Then
        '    e.Row.Cells.GetNamedItem("NewQuantity").Text = Format(Val(NewQuantity), "###,###")
        'End If
        'Dim ReplaceQuantity As String = e.Row.Cells.GetNamedItem("ReplaceQuantity").Text
        'If ReplaceQuantity <> "" Then
        '    e.Row.Cells.GetNamedItem("ReplaceQuantity").Text = Format(Val(ReplaceQuantity), "###,###")
        'End If
        'Dim PricePerUnit As String = e.Row.Cells.GetNamedItem("PricePerUnit").Text
        'If PricePerUnit <> "" Then
        '    e.Row.Cells.GetNamedItem("PricePerUnit").Text = Format(Val(PricePerUnit), "###,###.00")
        'End If
        'Dim Amount As String = e.Row.Cells.GetNamedItem("Amount").Text
        'If Amount <> "" Then
        '    e.Row.Cells.GetNamedItem("Amount").Text = "<font color='red'>" & Format(Val(Amount), "###,###.00") & "</font>"
        'End If
        Dim container As ISNet.WebUI.WebGrid.WebGridCellTemplateContainer
        Dim ValueLabel As Label
        Dim SignLabel As Label
        Dim ChangeLabel As Label
        Dim NewLabel As Label
        Dim ReplaceLabel As Label
        Dim UnitPriceLabel As Label
        Dim AmountLabel As Label
        Dim CurrentValue As Decimal
        Dim ChangeTo As Decimal
        Dim ChangeValue As Decimal
        container = e.Row.Cells(6).GetTemplateContainer()
        NewLabel = container.FindControl("CurrentValue")
        NewLabel.Text = Format(Val(NewLabel.Text), "###,##0")
        NewLabel.ForeColor = Drawing.Color.Black

        container = e.Row.Cells(7).GetTemplateContainer()
        ReplaceLabel = container.FindControl("CurrentValue")
        ReplaceLabel.Text = Format(Val(ReplaceLabel.Text), "###,##0")
        ReplaceLabel.ForeColor = Drawing.Color.Black

        container = e.Row.Cells(9).GetTemplateContainer()
       

        If TabContainer3.ActiveTab.ID = TabPanel6.ID And Session("test") = 1 Then
            CurrentValue = Val(Replace(e.Row.Cells(9).Value, ",", ""))
            ChangeTo = Val(Session("ChangeValue"))
            
            If AmountChangeCombo.Value = "T1" Then
                ChangeValue = ChangeTo
            ElseIf AmountChangeCombo.Value = "T2" Then
                ChangeValue = RoundNum(CurrentValue + (CurrentValue * ChangeTo / 100))
            ElseIf AmountChangeCombo.Value = "T3" Then
                ChangeValue = RoundNum(CurrentValue - (CurrentValue * ChangeTo / 100))
            End If
            If ChangeValue >= CurrentValue Then
                e.Row.Cells(10).Style.ForeColor = Drawing.Color.Black
            Else
                e.Row.Cells(10).Style.ForeColor = Drawing.Color.Red
            End If
            e.Row.Cells(10).Text = Format(ChangeValue, "###,##0")
            e.Row.Cells(10).Value = ChangeValue
        End If


        container = e.Row.Cells(8).GetTemplateContainer()
        UnitPriceLabel = container.FindControl("CurrentValue")
        UnitPriceLabel.Text = Format(Val(UnitPriceLabel.Text), "###,##0")
        UnitPriceLabel.ForeColor = Drawing.Color.Black
        SignLabel = container.FindControl("Sign")
        SignLabel.Visible = False
        ChangeLabel = container.FindControl("ChangeValue")
        ChangeLabel.Visible = False

        If TabContainer3.ActiveTab.ID = TabPanel7.ID And Session("test") = 1 Then
            CurrentValue = Val(Replace(UnitPriceLabel.Text, ",", ""))
            ChangeTo = Val(Session("ChangeValue"))
            ChangeLabel.Visible = True
            SignLabel.Visible = True
            If UnitPriceChangeCombo.Value = "T1" Then
                ChangeValue = ChangeTo
            ElseIf UnitPriceChangeCombo.Value = "T2" Then
                ChangeValue = RoundNum(CurrentValue + (CurrentValue * ChangeTo / 100))
            ElseIf UnitPriceChangeCombo.Value = "T3" Then
                ChangeValue = RoundNum(CurrentValue - (CurrentValue * ChangeTo / 100))
            End If
            If ChangeValue >= CurrentValue Then
                ChangeLabel.ForeColor = Drawing.Color.Black
            Else
                ChangeLabel.ForeColor = Drawing.Color.Red
            End If
            ChangeLabel.Text = Format(ChangeValue, "###,##0.00")
            Dim AmountValue As Decimal

            AmountValue = Val(Replace(ChangeLabel.Text, ",", "")) * (Val(Replace(NewLabel.Text, ",", "")) + Val(Replace(ReplaceLabel.Text, ",", "")))

            
            ChangeLabel.Visible = True
            SignLabel.Visible = True
            CurrentValue = Val(Replace(e.Row.Cells(9).Value, ",", ""))
            If AmountValue >= CurrentValue Then
                e.Row.Cells(10).Style.ForeColor = Drawing.Color.Black
            Else
                e.Row.Cells(10).Style.ForeColor = Drawing.Color.Red
            End If

            e.Row.Cells(10).Text = Format(AmountValue, "###,##0")
            e.Row.Cells(10).Value = AmountValue
        End If
        If TabContainer3.ActiveTab.ID = TabPanel5.ID And Session("test") = 1 Then
            If ItemCombo.Value = "Fct" Then
                e.Row.Cells.GetNamedItem("FundCenter").Text = e.Row.Cells.GetNamedItem("FundCenter").Text & "/" & ItemListCombo.Text
            ElseIf ItemCombo.Value = "Cmt" Then
                e.Row.Cells.GetNamedItem("Cmmt").Text = e.Row.Cells.GetNamedItem("Cmmt").Text & "/" & ItemListCombo.Text
            ElseIf ItemCombo.Value = "Pjt" Then
                e.Row.Cells.GetNamedItem("Project").Text = e.Row.Cells.GetNamedItem("Project").Text & "/" & ItemListCombo.Text
            ElseIf ItemCombo.Value = "Atp" Then
                e.Row.Cells.GetNamedItem("ActivityType").Text = e.Row.Cells.GetNamedItem("ActivityType").Text & "/" & ItemListCombo.Text
            ElseIf ItemCombo.Value = "Prd" Then
                e.Row.Cells.GetNamedItem("Product").Text = e.Row.Cells.GetNamedItem("Product").Text & "/" & ItemListCombo.Text
            ElseIf ItemCombo.Value = "Act" Then
                e.Row.Cells.GetNamedItem("Activity").Text = e.Row.Cells.GetNamedItem("Activity").Text & "/" & ItemListCombo.Text

            End If
        End If

    End Sub

    Protected Sub TestBtn_Click(sender As Object, e As ImageClickEventArgs) Handles TestBtn.Click
        Session("test") = 1
        If TabContainer3.ActiveTab.ID = TabPanel5.ID Then
            Session("ChangeValue") = ItemListCombo.Value.ToString
        ElseIf TabContainer3.ActiveTab.ID = TabPanel6.ID Then
            Session("ChangeValue") = Val(Replace(AmountChangeTextBox.Text, ",", "")).ToString
        Else
            Session("ChangeValue") = Val(Replace(UnitPriceChangeTextBox.Text, ",", "")).ToString
        End If
        GetData()
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        Session("test") = 0
        If TabContainer3.ActiveTab.ID = TabPanel5.ID Then
            If ItemListCombo.Value = "" Then
                scripttext = "<script>alert('กรุณาระบุค่าที่ต้องการเปลี่ยนแปลง');</script>"
                Exit Sub
            End If
            Session("ChangeValue") = ItemListCombo.Value.ToString
        ElseIf TabContainer3.ActiveTab.ID = TabPanel6.ID Then
            Session("ChangeValue") = Val(Replace(AmountChangeTextBox.Text, ",", "")).ToString
        Else
            Session("ChangeValue") = Val(Replace(UnitPriceChangeTextBox.Text, ",", "")).ToString
        End If
        Dim subsql As String = ""
        subsql = " select id from budgetdetails "
        subsql += ComposeSQL()
        Dim MoreUpdate As Integer = 0
        Dim sql As String = ""
        sql = " update budgetdetails set "

        If TabContainer3.ActiveTab.ID = TabPanel5.ID Then
            If ItemCombo.Value = "Fct" Then
                sql += " fundcenter_fundcentercode = '" & Session("ChangeValue") & "' "

                logger.Info("User " & Session("users") & " do mass change fund center with " & Session("ChangeValue"))
            ElseIf ItemCombo.Value = "Cmt" Then
                sql += " Cmmt_CmmtCode = '" & Session("ChangeValue") & "' "

                logger.Info("User " & Session("users") & " do mass change commitment with " & Session("ChangeValue"))
            ElseIf ItemCombo.Value = "Pjt" Then
                sql += " Project_ProjectCode = '" & Session("ChangeValue") & "' "
                Dim padap As New BasicDataTableAdapters.ProjectsTableAdapter
                Dim pfctr As String = padap.GetFCTR(Session("ChangeValue"), Val(PlanYearsTxt.Text))
                sql += " ,Project_FundcenterCode = '" & If(pfctr = "null", Nothing, pfctr) & "' "

                logger.Info("User " & Session("users") & " do mass change project code and fund center with " & Session("ChangeValue"))
            ElseIf ItemCombo.Value = "Act" Then
                sql += " Activity_ActivityCode = '" & Session("ChangeValue") & "' "

                logger.Info("User " & Session("users") & " do mass change activity with " & Session("ChangeValue"))
            ElseIf ItemCombo.Value = "Atp" Then
                sql += " ActivityType_ActivityTypeCode = '" & Session("ChangeValue") & "' "

                logger.Info("User " & Session("users") & " do mass change activity type with " & Session("ChangeValue"))
            ElseIf ItemCombo.Value = "Prd" Then
                sql += " Product_ProductCode = '" & Session("ChangeValue") & "' "

                logger.Info("User " & Session("users") & " do mass change product with " & Session("ChangeValue"))
            End If
        ElseIf TabContainer3.ActiveTab.ID = TabPanel6.ID Then
            If AmountChangeCombo.Value = "T1" Then
                sql += " Amount = " & RoundNum(Val(Replace(AmountChangeTextBox.Text, ",", "")))
            ElseIf AmountChangeCombo.Value = "T2" Then
                sql += " Amount =  CEILING((Amount + ( Amount / 100 * " & Session("ChangeValue") & " ))/100)*100 "
            ElseIf AmountChangeCombo.Value = "T3" Then
                sql += " Amount =  CEILING((Amount - ( Amount / 100 * " & Session("ChangeValue") & " ))/100)*100 "
            End If

            logger.Info("User " & Session("users") & " do mass change amount with " & AmountChangeTextBox.Text)
        ElseIf TabContainer3.ActiveTab.ID = TabPanel7.ID Then
            If UnitPriceChangeCombo.Value = "T1" Then
                sql += " PricePerUnit = " & RoundNum(Val(Replace(UnitPriceChangeTextBox.Text, ",", "")))
            ElseIf UnitPriceChangeCombo.Value = "T2" Then
                sql += " PricePerUnit =  CEILING((PricePerUnit + ( PricePerUnit / 100 * " & Session("ChangeValue") & " ))/100)*100 "
            ElseIf UnitPriceChangeCombo.Value = "T3" Then
                sql += " PricePerUnit =  CEILING((PricePerUnit - ( PricePerUnit / 100 * " & Session("ChangeValue") & " ))/100)*100 "
            End If
            MoreUpdate = 1

            logger.Info("User " & Session("users") & " do mass change unit price with " & UnitPriceChangeTextBox.Text)
        End If
        sql += " where id in (" & subsql & ")"
        'Response.Write(sql)
        'Exit Sub
        Dim adapter As New MassChangeDSTableAdapters.BudgetDetailsTableAdapter
        adapter.Execute(sql)
        If MoreUpdate = 1 Then
            sql = " update budgetdetails set "
            sql += " Amount = round((isnull(NewQuantity,0) + isnull(ReplaceQuantity,0)) * isnull(PricePerUnit,0) ,0) "
            sql += " where id in (" & subsql & ")"
            adapter.Execute(sql)
        End If

        logger.Info(sql)

        GetData()
    End Sub

    Function RoundNum(num As Decimal) As Decimal
        Return Math.Ceiling(num)
    End Function
End Class
